package com.ccp.dev.qbdms.config;

import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.system.configuration.SpringMvcConfig;
import com.uwyn.jhighlight.fastutil.Hash;
import freemarker.ext.jsp.TaglibFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 匿名访问配置类
 */
@Component
public class UrlConfig {

    @Resource
    private FreeMarkerConfigurer freeMarkerConfigurer;
    /**
     * 此处的springMvcConfig 未使用 但是必须添加, 它影响tld的加载顺序
     */
    @Resource
    private SpringMvcConfig springMvcConfig;

    @Autowired
    public void addAnonymousUrls(HashSet<String> anonymousUrls) {
        //增加具有匿名访问权限的url
        anonymousUrls.add("/test/httpClient/test");
        anonymousUrls.add("/es/dict");
        anonymousUrls.add("/extend/attachFile/download");

    }

    @Autowired
    public void addStaticResource(HashSet<String> staticResource){
        staticResource.add("/"+ZhiLiangConst.URL_API+"**");
    }


    @Autowired
    public void addRestUrls(HashSet<String> restUrls) {
        //增加具有rest风格的url
        //restUrls.add("/qbdms/");
        restUrls.add("/importTemplates/");
        restUrls.add("/pdfjs/");
        restUrls.add("/Document/");

    }

    @Autowired
    public void addDoNotXssFilter(HashSet<String> doNotXssFilter) {
        //增加不需要xss过滤的url
//        doNotXssFilter.add("/**");
    }

    @PostConstruct
    public void freeMarkerConfigurer() {
        List<String> tlds = new ArrayList<>();
        tlds.add("/static/qbdmsStatic/tld/HtTag.tld");
        tlds.add("/static/qbdmsStatic/tld/secSelectTag.tld");
        tlds.add("/static/qbdmsStatic/tld/DicTag.tld");
        tlds.add("/static/qbdmsStatic/tld/fillDeptTag.tld");
        tlds.add("/static/qbdmsStatic/tld/SourceDeptTag.tld");
        tlds.add("/static/qbdmsStatic/tld/DutyDeptTag.tld");
        TaglibFactory taglibFactory = freeMarkerConfigurer.getTaglibFactory();
        taglibFactory.setClasspathTlds(tlds);

        if (taglibFactory.getObjectWrapper() == null) {
            taglibFactory.setObjectWrapper(freeMarkerConfigurer.getConfiguration().getObjectWrapper());
        }
    }
}
